﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Raimu_ver2
{
    public partial class ListForm : Form
    {
        raimuCore raimu = new raimuCore();
        public Index form = new Index();
        private int counter = 0;

        public ListForm()
        {
            InitializeComponent();
            raimu.addList(new bugData());
            listView1.ListViewItemSorter = new ListViewItemComparer(3);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            form.Show();
            Debug.rm = this;
        }

        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            button1.Enabled = true;
            counter++;
            form.label3.Text = "TURN:" + counter.ToString();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            backgroundWorker1.RunWorkerAsync();
        }

        private void doWork()
        {

            raimu.doWork();
            List<bugData> temp = raimu.getList();
            Invoke((MethodInvoker)delegate()
            {
                this.listView1.Items.Clear();
            });
            foreach (bugData tmp in temp)
            {
                doTop[] dt = tmp.getLists();
                doTop[] dt2 = tmp.getListsToDo();
                string DoS = "";
                string DoS2 = "";

                foreach (doTop dd in dt)
                {
                    DoS += " " + dd.doName;
                }
                foreach (doTop dd in dt2)
                {
                    DoS2 += " " + dd.doName;
                }



                if (DoS.Replace(" ", "") == "")
                    DoS = "NONE";
                if (DoS2.Replace(" ", "") == "")
                    DoS2 = "NONE";
                if (DoS == "NONE" && DoS2 == "NONE")
                {
                    raimu.Delete(tmp);
                    DoS = "NONE(DELQUEUE)";
                    DoS2 = "NONE(DELQUEUE)";
                }
                string[] DoSS = 
                { 
                    DoS, 
                    DoS2,
                    tmp.id, 
                    tmp.ToString(),
                    tmp.changed.ToString(),
                    tmp.hp.ToString(),
                    tmp.oldHp.ToString() ,
                    tmp.getNetworkCount().ToString()
                };
                Invoke(
                    (MethodInvoker)delegate()
                    {
                        this.listView1.Items.Add(new ListViewItem(DoSS));
                        form.label1.Text = "ADD : " + raimu.add;
                        form.label2.Text = "DEL : " + raimu.down;
                    });
                
                

            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1_Click(null, null);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            this.doWork();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            raimu = new raimuCore();
            raimu.addList(new bugData());
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = listView1.FocusedItem.SubItems[2].Text;
            form.listView1.Items.Clear();
            foreach (bugData bd in raimu.getList())
            {
                if (bd.id == value)
                {
                    foreach (Neuron nd in bd.getNeuron())
                    {
                        string[] datas = {nd.key,nd.wdo.doName,nd.trust.ToString() };
                        form.listView1.Items.Add(new ListViewItem(datas));
                    }
                }
            }

        }



    }
    /// <summary>
    /// ListViewの項目の並び替えに使用するクラス
    /// </summary>
    public class ListViewItemComparer : System.Collections.IComparer
    {
        private int _column;

        /// <summary>
        /// ListViewItemComparerクラスのコンストラクタ
        /// </summary>
        /// <param name="col">並び替える列番号</param>
        public ListViewItemComparer(int col)
        {
            _column = col;
        }

        //xがyより小さいときはマイナスの数、大きいときはプラスの数、
        //同じときは0を返す
        public int Compare(object x, object y)
        {
            //ListViewItemの取得
            ListViewItem itemx = (ListViewItem)x;
            ListViewItem itemy = (ListViewItem)y;

            //xとyを文字列として比較する
            return string.Compare(itemx.SubItems[_column].Text,
                itemy.SubItems[_column].Text);
        }
    }
}